#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
   This file is part of MyServerTalks.

    MyServerTalks is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MyServerTalks is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MyServerTalks; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
'''
import sys
import os
#import popen2
import subprocess
from MySTBase import *
from MySTLog import *
from MySTReply import *

class MySTModuleExternal:
	
	def __init__(self, config):
		self.config = config
	
	def execute(self, instruct):
		# get the command
		extension = instruct.getCommand().lower()
		#try:
			# File to call
			file = self.config.get('modules', extension, 'call')
			print file

			# normalize path separators
			fullpath = os.path.normpath(MySTBase.getExternalExtensionDir()+file)
			print fullpath

			# Verify if file exists
			if (file == '' or not os.path.exists(fullpath)):
				raise Exception('Path not exists, please check and try again.')

			# Add commas to path for directories with spaces
			fullpath = '"'+fullpath+'"'

			# Execute the command
			#stdout, stdin, stderr = popen2.popen3(fullpath+' '+instruct.__str__())

			user_cmd = '%s %s' % (fullpath, instruct.__str__())
			print 'user_cmd: ' + user_cmd

			# call user command
			output_cmd = subprocess.Popen(user_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
			print output_cmd

			# get response from command
			stdout = output_cmd.stdout.read()
			stdin  = output_cmd.stdin.read()
			stderr = output_cmd.stderr.read()

			print stdout
			print stdin
			print stderr

			# Read the output
			output = "\n".join(stdout.readlines())

			stdout.close(); stdin.close(); stderr.close();

			MySTLog.log('Running external module: '+extension+' ('+file+')')
		#except:
			#output = MySTBase.getCommandNotFoundMessage()

		# call the reply object
		return MySTReply(MySTBase.getSucessCode(), output)
